// OGoMojo
// Whitemice.MOGI.RPC2.Company
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using System.Collections;
using CookComputing.XmlRpc;

namespace Whitemice.MOGI.RPC2 
{
	/// \brief The base class of contact and enterprise objects
	/// \note All contact type objects are owned
	public abstract class Company : OwnedDocument
	{
		/// \brief Constructor
		/// \param document An XmlRpcStruct document
		/// \param server Reference to server object	
		public Company(XmlRpcStruct document, Whitemice.MOGI.RPC2.Server server) :
			base(document, server) 
		{
		}

		/// \brief Provides the company number
		/// \note This value is derived from the id attribute
		///public new virtual string Number 
		///{ 
		///	get 
		///	{
		///		string id = (string)this.document["id"];
		///		return id.Substring(id.LastIndexOf('/') + 1);
		///	}
		///}
		
		/// \brief Accessor for the url value
		public string URL 
		{
			get 
			{
				Log.Debug("URL == " + (string)document["url"]);
				if (document.ContainsKey("url"))
					return (string)document["url"];
				return "";
			}
			set 
			{
				Log.Debug("URL = " + (string)document["url"]);
				if (document.ContainsKey("url"))
					document["url"] = value;
					else document.Add("url", value);
			}
		}
		
		/// \brief Return true if the company has the specified address type
		/// \param addressType Type of address to check for
		public bool HasAddressType(string addressType) 
		{
			Log.Debug("HasAddressType(" + addressType + ")");
			if (((XmlRpcStruct)document["addresses"]).Contains(addressType))
				return true;
				else return false;
		}
		
		public IAddress Address(string addressType)
		{
			Log.Debug("Address(" + addressType + ")");
			if (server.SupportsAddress(Type, addressType))
			{
				Log.Debug("Address(" + addressType + ") : Supported");
				if (!HasAddressType(addressType))
					((XmlRpcStruct)document["addresses"]).Add(addressType, server.AssembleNewAddress(addressType));
				return new Address((XmlRpcStruct)((XmlRpcStruct)document["addresses"])[addressType], this, addressType, server);
			}
			return null;
		}
		
		/// \brief Return true if the company has the specified phone type
		/// \param phoneType Type of phone number to check for
		public bool HasPhoneType(string phoneType) 
		{
			Log.Debug("HasPhoneType(" + phoneType + ")");
			if (((XmlRpcStruct)document["phones"]).Contains(phoneType))
				return true;
				else return false;
		}
		
		public IPhoneNumber PhoneNumber(string phoneType)
		{
			Log.Debug("PhoneNumber(" + phoneType + ")");
			if (server.SupportsPhone(Type, phoneType))
			{
				if (!HasPhoneType(phoneType))
					((XmlRpcStruct)document["phones"]).Add(phoneType, server.AssembleNewPhone(phoneType));
				return new PhoneNumber((XmlRpcStruct)((XmlRpcStruct)((XmlRpcStruct)document)["phones"])[phoneType], this, phoneType, server);
			}
			return null;
		}
		
		public Array Categories 
		{
			get
			{
				Log.Debug("Categories ==");
				if (document.ContainsKey("keywords"))
					if (document["keywords"] == null)
						throw new NoValueException();
						else return (Array)((string)document["keywords"]).Split(',');
					else throw new NoKeyException();			
			}
			set
			{
				Log.Debug("Categories =");
				string categories = "";
				foreach(string category in value) 
				{
					if (categories.Length > 0)
						categories = categories + ",";
					categories = categories + category;		
				}
				document["keywords"] = categories;
			}
		}
		
		public string Email2
		{
			get
			{
				Log.Debug("Email2 ==");
				if (HasExtendedAttribute("email2"))
					return (string)ExtendedAttribute("email2");
				return "";
			}
			set
			{
				Log.Debug("Email2 =");
				ExtendedAttribute("email2", value);
			}
		}
		
		public string Email3
		{
			get
			{
				Log.Debug("Email3 ==");
				if (HasExtendedAttribute("email3"))					
					return (string)ExtendedAttribute("email3");
				return "";
			}
			set
			{
				Log.Debug("Email3 =");
				ExtendedAttribute("email3", value);
			}
		}
		
		public string Comment
		{
			get 
			{
				Log.Debug("Comment == " + (string)document["comment"]);
				if (document.ContainsKey("comment"))
					return (string)document["comment"];
				return "";
			}
			set 
			{
				Log.Debug("Comment = " + (string)document["comment"]);
				if (document.ContainsKey("comment"))
					document["comment"] = value;
					else document.Add("comment", value);
			}
		}
	}
}